﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Aj.Core;

public partial class Manager_CategoryEdit : AuthPageBase
{
    protected int id;
    protected int pid;
    protected string mname = "";
    protected int enable = 1;

    protected DataSet ds = null;
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        try
        {
            id = QueryParamInt32("id");
            pid = QueryParamInt32("pid");
            if (id > 0)
            {
                ds = CustomQuery.Category_GetCategoryById(id);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    mname = ds.Tables[0].Rows[0]["C_Name"].ToString();
                    pid = Convert.ToInt32(ds.Tables[0].Rows[0]["C_Pid"]);
                    //enable = Convert.ToInt32(ds.Tables[0].Rows[0]["C_Enable"]);
                }
            }

            if (IsPost)
            {
                // 更新
                if (id > 0)
                {
                    if (ds != null && ds.Tables.Count > 0)
                    {
                        int rel = CustomQuery.Category_Update(id, FormParamInt32("pid"), FormParam("mname"), enable);
                        if (rel > 0)
                        {
                            Function.RemoveCacheObject("/cache/menu/" + FormParamInt32("pid"));
                            Page_GoTo("Category.aspx", true);
                        }
                        else
                        {
                            MessageBox("修改失败！");
                        }
                    }
                }
                else // 新建
                {
                    object obj = CustomQuery.Category_GetCategorySort(pid.ToString());
                    int sort = DBNull.Value != obj && null != obj ? Convert.ToInt32(obj) : 0;
                    int rel = CustomQuery.Category_Add(pid, FormParam("mname"), sort, enable);
                    if (rel > 0)
                    {
                        Function.RemoveCacheObject("/cache/menu/" + pid);
                        Page_GoTo("Category.aspx", true);
                    }
                    else
                    {
                        MessageBox("新建失败！");
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Utility.WriteErrorLog(ex);
        }
    }
}